package main

import (
	"fmt"
	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/alibaba/sentinel-golang/core/base"
	"github.com/alibaba/sentinel-golang/core/flow"
	"log"
)

func main() {
	err := sentinel.InitDefault()
	if err != nil {
		log.Fatalf("初始化sentinel失败,err:%v", err)
	}
	_, err = flow.LoadRules([]*flow.Rule{
		{
			Resource:               "qps-test",
			TokenCalculateStrategy: flow.Direct,
			ControlBehavior:        flow.Reject,
			Threshold:              10,
			StatIntervalInMs:       1000,
		},
	})
	if err != nil {
		log.Fatalf("加载规则失败,err:%v", err)
	}

	for i := 0; i < 12; i++ {
		entry, blockError := sentinel.Entry("qps-test", sentinel.WithTrafficType(base.Inbound))
		if blockError != nil {
			fmt.Println("检查失败")
		} else {
			fmt.Println("检查通过")
			entry.Exit()
		}
	}
}
